//
//  PageManager.m
//  HealthCard
//
//  Created by leon on 13-9-9.
//  Copyright (c) 2013年 xxx. All rights reserved.
//

#import "PageManager.h"
#import "PMNavigationController.h"
#import "PMViewController.h"
//#import "ImageUtils.h"
#import "PMTabBarController.h"

#define TAB_COUNT_HIDETABBAR                        1


@interface PageManager()
@end

@implementation PageManager
@synthesize naviCfgURL = _naviCfgURL;
@synthesize pageCfgURL = _pageCfgURL;
@synthesize root = _root;
@synthesize useMutilLayer = _useMutilLayer;
@synthesize naviBarTextColor = _naviBarTextColor;
@synthesize naviBarBackImage = _naviBarBackImage;
@synthesize naviBarBackColor = _naviBarBackColor;
@synthesize tabBarBackImage = _tabBarBackImage;
@synthesize pmNavi  = _pmNavi;

+ (PageManager*)shareInstance
{
    static PageManager* manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PageManager alloc] init];
    });
    return manager;
}

#pragma mark super method
- (id)init
{
    if(self = [super init]){
        _useMutilLayer = NO;
        _naviBarTextColor = [UIColor whiteColor];
        _naviBarBackImage = nil;
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}

#pragma mark private method
//- (void)initConfigInfo
//{
//    [self createDataStackCfg];
//    [self createDataPageCfg];
//}

- (void)switchStackTo:(PMNavigationController*)stack
{
    if(_curStack == stack)
        return;

//    switch (stack.stackInfo.type) {
//        case NaviType_Normal:{
//            if(_curStack.stackInfo.type != NaviType_None){
//                if(_curStack.stackInfo.type == NaviType_Pop){
//                    [_curStack dismissViewControllerAnimated:YES completion:nil];
//                }
//                if(_root.selectedViewController != stack){
//                    [_root setSelectedViewController:stack];
//                }
//                _curStack = stack;
//            }
//            break;
//        }
//        case NaviType_Pop:
//        {
//            if(_curStack.stackInfo.type != NaviType_None){
//                if(_curStack.stackInfo.type == NaviType_Normal){
                    if([_curStack.topViewController isKindOfClass:[PMViewController class]]){
                        [(PMViewController*)_curStack.topViewController setIsPush:NO];
                    }
                    [_root presentViewController:stack animated:YES completion:nil];
//                }
//                if(_root.presentedViewController != stack){
//                    [_root.presentedViewController dismissViewControllerAnimated:YES completion:nil];
//                    [_root presentViewController:stack animated:YES completion:nil];
//                }
//                _curStack = stack;
//            }
//            break;
//        }
//        default:
//        {
//            break;
//        }
//    }
}


#pragma mark public method


- (void)gotoPage:(PMViewController*)goViewController selfView:(UIViewController*)selfView
{
    [self gotoPage:goViewController withParams:nil selfView:selfView];
}

- (void)gotoPage:(PMViewController *)goViewController withParams:(id)params selfView:(UIViewController*)selfView
{
    [self gotoPage:goViewController withParams:params withBackPageView:nil selfView:selfView];
}

- (void)gotoPage:(PMViewController*)goViewController withBackPageView:(PMViewController*)backPageView selfView:(UIViewController*)selfView
{
    [self gotoPage:goViewController withParams:nil withBackPageView:backPageView selfView:selfView];
}

- (void)gotoPage:(PMViewController *)goViewController
      withParams:(NSMutableDictionary*)params
  withBackPageView:(PMViewController*)backPageView selfView:(UIViewController*)selfView
{
    goViewController.parameters = params;
    goViewController.backPageView = backPageView;
    [selfView.navigationController pushViewController:goViewController animated:YES];
}


- (void)gotoPagePresentView:(PMViewController *)goViewController selfView:(UIViewController*)selfView
{
    [selfView.navigationController presentViewController:goViewController animated:YES completion:nil];
}

- (void)gotoBackPage:(PMViewController *)pmViewController
{
    if (pmViewController.backPageView) {
        for (PMViewController *pmview in pmViewController.navigationController.viewControllers) {
            if ([pmview isKindOfClass:[pmViewController.backPageView class]]) {
                [pmViewController.navigationController popToViewController:pmview animated:YES];
                return;
            }
        }
        
#warning 目前返回只能返回viewControllers中已有的view，如果不存在请设置跳转后人工返回。
        [pmViewController.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
   [pmViewController.navigationController popToViewController:[pmViewController.navigationController.viewControllers objectAtIndex:([pmViewController.navigationController.viewControllers count] -2)] animated:YES];
}

- (void)setUseMutilLayer:(BOOL)useMutilLayer
{
    if(_navigationPool && [_navigationPool count] > 0){
        NSEnumerator* enumerator = [_navigationPool objectEnumerator];
        id obj = nil;
        while (obj = [enumerator nextObject]) {
            if([obj isKindOfClass:[PMNavigationController class]]){
                [((PMNavigationController*)obj) setUseMutilLayer:useMutilLayer];
            }
        }
    }
    _useMutilLayer = useMutilLayer;
}

#pragma mark UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController
 didSelectViewController:(UIViewController *)viewController
{
    if([viewController isKindOfClass:[PMNavigationController class]]){
        _curStack = (PMNavigationController*)viewController;
    }
}


//禁用tabBarController双击回到root视图
- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)viewController {
    UIViewController *tbSelectedController = tbc.selectedViewController;
    if ([tbSelectedController isEqual:viewController]) {
        return NO;
    }

    PMNavigationController* navi = (PMNavigationController*)viewController;
    if([[navi viewControllers] count] <= 0){
        [MixProgressHUD dismiss];
        [MixProgressHUD showError:@"该功能正在开发中" afterDelay:0.1];
        return NO;
    }
    return YES;
}

#pragma mark UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        if([navigationController.viewControllers count] > 1){
            navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        else{
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}


#pragma mark - 跳转到首页

-(void)gotoPageRootViewController:(PMViewController *)selfView{
    dispatch_async(dispatch_get_main_queue(), ^{
        selfView.tabBarController.tabBar.hidden = NO;
    });
    selfView.tabBarController.selectedIndex = 0;
    [selfView.navigationController popToRootViewControllerAnimated:YES];
    
}

@end
